package com.pan.structual.flyweight.entiy;

import com.pan.structual.flyweight.utils.XmlUtils;

import java.awt.*;
import java.awt.image.BufferedImage;

public class CharMyImage implements MyImage {

    String key;

    BufferedImage charImage;

    public CharMyImage(String ch){
        //打印出来确定创建次数
        System.out.println("test create charMyImage: \""+ch+"\"");
        if (ch.length() != 1){
            throw new RuntimeException("charImage只能有一个字母");
        }
        this.key = ch;
        int[] pictureWidthAndHeight = XmlUtils.INSTANCE.getPictureWidthAndHeight();
        int width = pictureWidthAndHeight[0];
        int height = pictureWidthAndHeight[1];
        charImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = charImage.createGraphics();
        Font font = XmlUtils.INSTANCE.getFont();
        Color fontColor = XmlUtils.INSTANCE.getFontColor();
        Color backgroundColor = XmlUtils.INSTANCE.getBackgroundColor();
        graphics.setBackground(backgroundColor);
        graphics.setColor(fontColor);
        graphics.setFont(font);
        graphics.drawString(ch, 2,XmlUtils.INSTANCE.getFontSizeAndStyle("Size")+2);
        graphics.dispose();
    }

    public String getKey() {
        return key;
    }

    public BufferedImage getImage() {
        return charImage;
    }
}
